<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true">

      <div class="search">
        <div class="searchcopy">
          <el-form-item label="工单编号:">
            <el-input v-model="queryParams.orderNumber" placeholder="请输入工单编号" clearable size="small"
                      style="width: 240px" @keyup.enter.native="handleQuery" @keyup.native="keyUp" show-word-limit/>
          </el-form-item>
          <el-form-item label="产品图号:">
            <el-input v-model="queryParams.productCode" placeholder="请输入产品图号" clearable size="small"
                      style="width: 240px" @keyup.enter.native="handleQuery" @keyup.native="keyUp" show-word-limit/>
          </el-form-item>
          <el-form-item label="产线名称:">
            <el-select v-model="queryParams.prodLineName" placeholder="请输入产线名称" filterable clearable
                       style="width: 240px">
              <el-option v-for="item in lineName" :key="item.label" :label="item.label" :value="item.label"/>
            </el-select>
          </el-form-item>
          <el-form-item label="计划状态:">
            <el-select v-model="queryParams.status" placeholder="请选择任务状态" filterable clearable
                       style="width: 240px">
              <el-option v-for="item in stocklist" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </div>
        <div class="search-btn-style">
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </el-form>
    <div class="add-btn">
      <el-button type="primary" icon="el-icon-edit" size="small" @click="handleeditstatus">更改任务状态
      </el-button>
      <!--      <el-button type="primary" icon="el-icon-upload" size="small" @click="handleimport">计划导入-->
      <!--      </el-button>-->
      <el-button type="primary" icon="el-icon-download" size="small" @click="handleExcel">导出
      </el-button>
      <el-button type="primary" icon="el-icon-refresh" size="small" @click="handleanew">重新计算时间
      </el-button>
      <el-button type="primary" icon="el-icon-d-caret" size="small" @click="gomoveplan">移动计划
      </el-button>
      <el-button type="danger" icon="el-icon-delete" size="small" @click="handleDelete">删除
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <el-table v-loading="loading" :data="inprodplanList" @selection-change="handleSelectionChange"
              :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55"></el-table-column>
      <!--      <el-table-column label="sort" align="center" prop="sort" fixed="left" :show-overflow-tooltip="true"/>-->
      <el-table-column label="工单编号" align="center" prop="orderNumber" fixed="left" :show-overflow-tooltip="true"
                       width="150"/>
      <el-table-column label="产品图号" align="center" prop="productCode" fixed="left" :show-overflow-tooltip="true"
                       width="220"/>
      <el-table-column label="产线名称" align="center" prop="prodLineName" fixed="left" :show-overflow-tooltip="true"
                       width="200"/>
      <el-table-column label="工单数量" align="center" prop="orderProductCount" sortable :show-overflow-tooltip="true"
                       width="100"/>
      <el-table-column label="起始站点" align="center" prop="beginStationName"
                       :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column label="终点站点" align="center" width="160" prop="endStationName" :show-overflow-tooltip="true"
                       :formatter="endnameformatter">
      </el-table-column>
      <el-table-column label="终点库位区域" align="center" prop="endStationTypeId" :show-overflow-tooltip="true"
                       :formatter="endformatter" width="200">
      </el-table-column>
      <el-table-column label="空箱是否流转" align="center" :formatter="emptyBoxHomingformatter" prop="emptyBoxHoming"
                       :show-overflow-tooltip="true" width="100"/>
      <el-table-column label="计划状态" align="center" prop="status" :show-overflow-tooltip="true"
                       :formatter="statusformatter"/>
      <el-table-column label="配送时间" align="center" prop="sendTime" sortable :show-overflow-tooltip="true"
                       width="200">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.sendTime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建人" align="center" prop="creator" :show-overflow-tooltip="true"/>-->
      <el-table-column label="创建时间" align="center" prop="createTime" sortable :show-overflow-tooltip="true"
                       width="200">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="更新人" align="center" prop="updater" :show-overflow-tooltip="true"/>-->
      <!--      <el-table-column label="更新时间" align="center" prop="updateTime" sortable :show-overflow-tooltip="true"-->
      <!--                       width="180">-->
      <!--        <template v-slot="scope">-->
      <!--      <span>{{ parseTime(scope.row.sendTime) }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column width="240" label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="small" icon="el-icon-copy-document" type="text" @click="handleinsert(scope.row)">
            插单
          </el-button>
          <!--          <el-button size="small" icon="el-icon-top" type="text" @click="goup(scope.row)">-->
          <!--            上调-->
          <!--          </el-button>-->
          <!--          <el-button size="small" icon="el-icon-bottom" type="text" @click="godown(scope.row)">-->
          <!--            下调-->
          <!--          </el-button>-->
          <el-button size="small" icon="el-icon-back" type="text" @click="handleinadvance(scope.row)">
            提前
          </el-button>
          <el-button size="small" icon="el-icon-right" type="text" @click="handlepostpone(scope.row)">
            推迟
          </el-button>
          <el-button size="small" icon="el-icon-tickets" type="text" @click="handlerecord(scope.row)">
            操作记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!--更改任务状态-->
    <editstatus ref="editstatus" :title="editstatus.title" :linedatas="editstatus.linedatas"
                @refresh="refresh"></editstatus>
    <!--导入-->
    <tochannel ref="tochannel" :title="tochanneldata.title" @refresh="refresh"
               @getplannamedata="getplannamedata"></tochannel>
    <!--插单-->
    <interposition ref="interposition" :title="interposition.title" :id="interposition.id" :sort="interposition.sort"
                   :prodLineName="interposition.prodLineName" @refresh="refresh"
                   :beginStationName="interposition.beginStationName"
                   :sendTime="interposition.sendTime"></interposition>
    <!--提前-->
    <inadvance ref="inadvance" :title="inadvancedata.title" :linedata="inadvancedata.linedata"
               @refresh="refresh"></inadvance>
    <!--推迟-->
    <putoff ref="postpone" :title="postponedata.title" :linedata="postponedata.linedata"
            @refresh="refresh"></putoff>
    <!--推迟记录-->
    <eferredrecord ref="eferredrecord" :title="eferredrecorddata.title" :id="eferredrecorddata.id"></eferredrecord>
    <!--移动计划-->
    <moveplan ref="moveplan" :title="moveplan.title" :linedata="moveplan.linedata" @refresh="refresh"
              :ids="moveplan.ids"></moveplan>
  </div>
</template>

<script>
import editstatus from "@/views/prodManger/outProdplan/editstatus.vue";
import tochannel from "@/views/prodManger/outProdplan/tochannel.vue";
import interposition from "@/views/prodManger/outProdplan/interposition.vue";
import inadvance from "@/views/prodManger/outProdplan/inadvance.vue";
import putoff from "@/views/prodManger/outProdplan/putoff.vue";
import eferredrecord from "@/views/prodManger/outProdplan/eferredrecord.vue";
import moveplan from "@/views/prodManger/outProdplan/moveplan.vue";
import {getprodplanlist, delall, getExcel, updown, recalculate, getlinename} from "@/api/prodManger/outProdplan";
import {listData} from "@/api/system/dict/data";

export default {
  name: "Role",
  components: {
    "editstatus": editstatus,
    "tochannel": tochannel,
    "interposition": interposition,
    "inadvance": inadvance,
    "putoff": putoff,
    "eferredrecord": eferredrecord,
    "moveplan": moveplan,
  },
  data() {
    return {
      agvId: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产线下拉框名字
      lineName: null,
      // 列表
      inprodplanList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 200,
        orderNumber: null,
        productCode: null,
        prodLineName: null,
        status: null,
      },
      // 更改任务状态参数
      editstatus: {
        title: "",
        linedatas: null,
      },
      // 导入的参数
      tochanneldata: {
        title: ""
      },
      // 插单
      interposition: {
        title: "",
        id: null,
        sort: null,
        prodLineName: null,
        beginStationName: null,
        sendTime: null,
      },
      // 提前
      inadvancedata: {
        title: "",
        linedata: null,
      },
      // 推迟
      postponedata: {
        title: "",
        linedata: null,
      },
      // 推迟记录的参数
      eferredrecorddata: {
        title: "",
        id: null
      },
      //移动计划
      moveplan: {
        title: "",
        linedata: null,
        ids: null,
      },
      arr: [],//多选的id
      // 备料下线生产计划状态
      stocklist: null,
      stocklistcopy: null,
      // 库位类型
      positionlist: null,
    }
  },
  async mounted() {
    await this.getStatusList();
    await this.getPositionList();
    await this.getplannamedata();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      let today = new Date();
      let year = today.getFullYear();
      let month = today.getMonth() + 1; // 月份从0开始，所以需要加1
      let date = today.getDate();
      let monthzero = String(month).padStart(2, '0') //补零
      let datezero = String(date).padStart(2, '0') //补零
      let dateString = year + '-' + monthzero + '-' + datezero;
      let start = dateString + " 00:00:01"
      let end = dateString + " 23:59:59"
      let arr = []
      arr.push(start)
      arr.push(end)
      this.queryParams.createTime = arr;
      getprodplanlist(this.queryParams).then(
        response => {
          response.data.list.map(item => {
            item.middle = 1;
            let date = new Date(item.createTime);
            let year = date.getFullYear();
            let month = date.getMonth() + 1; // 月份从0开始，所以需要加1
            let day = today.getDate();
            let daycopy = today.getDate() + 1;
            let dateString = year + '-' + month + '-' + daycopy + " 08:00:00";
            let bijiao = new Date(dateString);
            let bijiaocopy = bijiao.getTime();
            if (item.sendTime > bijiaocopy) {
              item.middle = 0;
            }
          })
          this.inprodplanList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
          this.getplannamedata();
        }
      );
    },
    tableRowClassName({row, rowIndex}) {
      if (row.middle == 0) {
        return 'warning-row';
      }
      return '';
    },
    // 获取当天的产线名
    getplannamedata() {
      getlinename().then(res => {
        this.lineName = res.data.map(item => {
          return {'label': item}
        })
      })
    },
    // 数据字典
    getStatusList() {
      let data = {
        dictType: "task_status",
      };
      return listData(data).then((res) => {
        this.stocklistcopy = res.data.list;
        let middle = JSON.parse(JSON.stringify(res.data.list))
        middle.splice(3, 1);
        this.stocklist = middle;
      })
    },
    getPositionList() {
      let data = {
        dictType: "position_type",
      };
      return listData(data).then((res) => {
        this.positionlist = res.data.list;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.orderNumber = null;
      this.queryParams.productCode = null;
      this.queryParams.prodLineName = null;
      this.queryParams.status = null;
      this.handleQuery();
    },
    // 子组件刷新列表
    refresh() {
      this.getList();
    },
    // 多选事件
    handleSelectionChange(val) {
      this.arr = val
    },
    // 对数字进行升序排序
    areNumbersConsecutive(numbers) {
      numbers.sort((a, b) => a - b); // 对数字进行升序排序
      for (let i = 0; i < numbers.length - 1; i++) {
        if (numbers[i] + 1 !== numbers[i + 1]) {
          return false;
        }
      }
      return true;
    },
    // 移动位置
    gomoveplan() {
      if (this.arr.length == 0) {
        this.$message.warning('请选择要移动的计划')
        return;
      }
      let allstatus = this.arr.map(item => {
        return item.status;
      })
      let some = allstatus.some(item => {
        return item == 1
      })
      if (some) {
        this.$message.warning('配送中的数据不能移动位置');
        return;
      }
      let first = this.arr[0]?.prodLineName;
      let all = this.arr.every(item => {
        return item.prodLineName == first;
      });
      if (!all) {
        this.$message.warning('请对相同的产线生产计划进行移动')
        return;
      }
      let sortlist = this.arr.map(item => {
        return item.sort
      })
      let sortstatus = this.areNumbersConsecutive(sortlist)
      if (!sortstatus) {
        this.$message.warning('请对连续的生产计划进行移动')
        return;
      }
      let ids = this.arr.map(item => {
        return item.sort
      });
      let idscopy = [];
      for (let i = 0; i <= ids.length - 1; i++) {
        let obj = {
          id: ids[i]
        }
        idscopy.push(obj)
      }
      this.moveplan.title = "移动计划位置";
      this.moveplan.ids = idscopy;
      this.moveplan.linedata = JSON.parse(JSON.stringify(this.arr));
      this.$refs.moveplan.dialogVisible = true;
    },
    // 多选删除
    handleDelete() {
      if (this.arr.length == 0) {
        this.$message.warning("请选择要删除的计划");
      } else {
        let alldel = this.arr.every(item => {
          return item.status == 0 || 3
        })
        if (alldel) {
          let idslist = this.arr.map(item => {
            return item.id
          })
          let that = this;
          this.$modal.confirm("确定要删除吗？").then(function () {
            return delall(idslist).then(() => {
              that.queryParams.pageNo = 1;
              that.getList();
              that.$modal.msgSuccess("删除成功");
            });
          })
        } else {
          this.$message.warning('请对待配送或暂停的数据进行删除')
        }
      }
    },
    // 重新计算时间
    handleanew() {
      let isInTasks = "false";
      let that = this;
      this.$modal.confirm("确定要重新计算时间吗？").then(function () {
        recalculate(isInTasks).then(res => {
          that.$message.success('重新计算时间成功');
          that.getList();
        })
      })
    },
    // 导入
    handleimport() {
      this.tochanneldata.title = "导入下线生产计划";
      this.$refs.tochannel.upload.open = true;
    },
    // 多选导出
    handleExcel() {
      if (this.arr.length == 0) {
        this.$message.warning('请选择要导出的生产计划')
        return;
      } else {
        let ids = [];
        ids = this.arr.map(item => {
          return item.id
        })
        this.$modal.confirm('是否确认导出选中下线生产计划?').then(() => {
          this.exportLoading = true;
          return getExcel(ids);
        }).then(response => {
          this.$download.excel(response, '下线生产计划.xls');
          this.exportLoading = false;
        }).catch(() => {
        });
      }
    },
    //更改任务状态
    handleeditstatus() {
      if (this.arr.length == 0) {
        this.$message.warning("请选择要修改状态的计划");
        return;
      }
      if (this.arr.length == 1) {
        this.editstatus.linedatas = this.arr;
        this.editstatus.title = "更改任务状态";
        this.$refs.editstatus.dialogVisible = true;
      } else {
        let line = this.arr[0].prodLineName;
        let allline = this.arr.every(item => {
          return item.prodLineName == line
        })
        if (!allline) {
          this.$message.warning("请对相同的产线进行更改状态");
          return;
        }
        let first = this.arr[0].status;
        let unify = this.arr.every(item => {
          return item.status == first
        })
        if (!unify) {
          this.$message.warning("请对相同状态的产线计划进行更改状态");
          return;
        } else {
          this.editstatus.linedatas = this.arr;
          this.editstatus.title = "更改任务状态";
          this.$refs.editstatus.dialogVisible = true;
        }
      }
    },
    // 插单
    handleinsert(row) {
      this.interposition.id = row.id;
      this.interposition.sort = row.sort;
      this.interposition.prodLineName = row.prodLineName;
      this.interposition.beginStationName = row.beginStationName;
      this.interposition.sendTime = row.sendTime;
      this.interposition.title = "插单";
      this.$refs.interposition.dialogVisible = true;
    },
    // 上调
    goup(row) {
      let alldata = {};
      let olddata = {};
      let newdata = {};
      if (this.queryParams.pageNo == 1 && this.inprodplanList.indexOf(row) == 0) {
        this.$message.warning('已经是第一个任务，无法上调')
        return;
      }
      if (this.queryParams.pageNo !== 1 && this.inprodplanList.indexOf(row) == 0) {
        this.$message.warning('这个任务是本页面第一条数据，请切换分页后再上调')
        return;
      }
      let upone = this.inprodplanList.indexOf(row) - 1
      if (row.prodLineName != this.inprodplanList[upone].prodLineName) {
        this.$message.warning('请对同一条产线的数据进行上调')
        return;
      }
      let that = this;
      this.$modal.confirm("确定要上调吗？").then(function () {
        olddata.id = row.id;
        olddata.sort = row.sort;
        newdata.id = that.inprodplanList[upone].id;
        newdata.sort = that.inprodplanList[upone].sort;
        alldata.taskA = olddata;
        alldata.taskB = newdata;
        updown(alldata).then(res => {
          that.getList();
          that.$message.success('上调成功')
        })
      })
    },
    // 下调
    godown(row) {
      let alldata = {};
      let olddata = {};
      let newdata = {};
      let num = this.inprodplanList.indexOf(row) + 1;
      let last = this.queryParams.pageSize * (this.queryParams.pageNo - 1) + num;
      if (last == this.total) {
        this.$message.warning('已经是最后一个任务，无法下调')
        return;
      }
      if (num == this.queryParams.pageSize) {
        this.$message.warning('这个任务是本页面最后一条数据，请切换分页后再下调')
        return;
      }
      let downone = this.inprodplanList.indexOf(row) + 1;
      if (row.prodLineName != this.inprodplanList[downone]?.prodLineName) {
        this.$message.warning('请对同一条产线的数据进行下调')
        return;
      }
      let that = this;
      this.$modal.confirm("确定要下调吗？").then(function () {
        olddata.id = row.id;
        olddata.sort = row.sort;
        newdata.id = that.inprodplanList[downone].id;
        newdata.sort = that.inprodplanList[downone].sort;
        alldata.taskA = olddata;
        alldata.taskB = newdata;
        updown(alldata).then(res => {
          that.getList();
          that.$message.success('下调成功')
        })
      })
    },
    // 提前
    handleinadvance(row) {
      if (row.status != 0) {
        this.$message.warning("只能对待配送的数据进行提前")
        return;
      }
      this.inadvancedata.title = '提前';
      this.inadvancedata.linedata = row;
      this.$refs.inadvance.dialogVisible = true;
    },
    // 推迟
    handlepostpone(row) {
      if (row.status != 0) {
        this.$message.warning("只能对待配送的数据进行推迟")
        return;
      }
      this.postponedata.title = '推迟';
      this.postponedata.linedata = row;
      this.$refs.postpone.dialogVisible = true;
    },
    // 推迟记录
    handlerecord(row) {
      this.eferredrecorddata.id = row.id;
      this.eferredrecorddata.title = "操作记录";
      this.$refs.eferredrecord.dialogVisible = true;
    },
    //处理列表数据
    emptyBoxHomingformatter(row) {
      if (row.emptyBoxHoming == false) {
        return "否"
      } else if (row.emptyBoxHoming == true) {
        return "是"
      }
    },
    statusformatter(row) {
      let data = this.stocklistcopy.filter(item => {
        return item.value == row.status
      })
      return data[0]?.label
    },
    endformatter(row) {
      let data = this.positionlist?.filter(item => {
        return item.value == row.endStationTypeId
      })
      return data[0]?.label
    },
    endnameformatter(row) {
      if (row.endStationName == null) {
        return "暂时没有终点站点"
      } else {
        return row.endStationName
      }
    }
  }
};
</script>

<style scoped>
/deep/ .el-table .warning-row {
  background: #ffdede;
}

.searchcopy {
  background-color: #fff;
  padding: 0 20px;
  padding-top: 17px;
  border-radius: 4px;
  display: inline-block;
}

.issue-pagination {
  margin-top: 26px;
}

/deep/ .el-dialog {
  width: 790px !important;
}

.success {
  display: inline-block;
  width: 45px;
  height: 30px;
  line-height: 30px;
  background-color: #eafff1;
  color: #38b865;
  border: 1px solid #4dc075;
  border-radius: 4px;
}

.fail {
  display: inline-block;
  width: 45px;
  height: 30px;
  line-height: 30px;
  background-color: #fff1ea;
  color: #eb5e12;
  border: 1px solid #ef7f43;
  border-radius: 4px;
}

.middle {
  display: inline-block;
  width: 45px;
  height: 30px;
  line-height: 30px;
  background-color: #fff8ea;
  color: #faad14;
  border: 1px solid #faad14;
  border-radius: 4px;
}

.search > * {
  padding: 0;
  margin-right: 30px !important;
}
</style>
